<template>
	<view>
		<view class="un-money-tou">
			<view class="un-money-tou-text">{{user.NAME}}</view>
		</view>
		
		<view class="un-money-item">
			<view class="un-money-item1">
				<text class="clamp un-money-item-text3">可用惠券</text>
				<text class="un-money-item-text1">{{result}}</text>
				<text class="clamp un-money-item-text3">最近7日获得惠券</text>
				<text class="un-money-item-text1">{{coupon}}</text>
			</view>
			<view class="un-money-item1">
				<text class="clamp un-money-item-text3" v-if="scale">兑换比例：{{scale}}：1</text>
				<text class="clamp un-money-item-text4" @click="exchange" v-if="user.ECOUPONSTATE==0&&ecoupon>0&&scale">兑换电子券</text>
				<text class="clamp un-money-item-text4" style="color: #999999;border: #999999 1upx solid;" v-if="user.ECOUPONSTATE==1">兑换中</text>
				<text class="clamp un-money-item-text4" style="color: #999999;border: #999999 1upx solid;" v-if="user.ECOUPONSTATE==0&&ecoupon==0&&scale">惠券不足</text>
				<text class="clamp un-money-item-text4" style="color: #999999;border: #999999 1upx solid;margin-top: 30upx;" v-if="!scale">兑换电子券</text>
			</view>
			<view class="un-money-item2">
				<text class="clamp un-money-item-text3">电子券</text>
				<text class="un-money-item-text1" v-if="user.ECOUPONSTATE==0&&scale">{{ecoupon}}</text>
				<text class="un-money-item-text1" v-else>{{user.ECOUPON}}</text>
				<text class="clamp un-money-item-text3" style="color: #E02E24;" v-if="user.ECOUPONSTATE==0&&ecoupon>0&&scale">可兑换</text>
				<text class="clamp un-money-item-text3" style="color: #F04844;" v-if="user.ECOUPONSTATE==1">审核中</text>
				<text class="clamp un-money-item-text3" style="color: #666666;" v-if="!scale">暂不可兑换</text>
			</view>
		</view>
		<view class="un-money-itema">
			<view class="un-money-item1">
				<text class="clamp un-money-item-text3">可用惠券</text>
				<text class="un-money-item-text1">{{result}}</text>
				<text class="clamp un-money-item-text3">最近7日获得惠券</text>
				<text class="un-money-item-text1">{{coupon}}</text>
			</view>
			<view class="un-money-item1">
				
				<text class="clamp un-money-item-text3" v-if="scale1">转换比例：{{scale1}}%</text>
				<text class="clamp un-money-item-text4" @click="exchange1" v-if="state!=1&&ecoupon1>0&&scale1">转换余额</text>
				<text class="clamp un-money-item-text4" style="color: #999999;border: #999999 1upx solid;" v-if="state==1">转换中</text>
				<text class="clamp un-money-item-text4" style="color: #999999;border: #999999 1upx solid;" v-if="state!=1&&ecoupon1==0&&scale1">惠券不足</text>
				<text class="clamp un-money-item-text4" style="color: #999999;border: #999999 1upx solid;margin-top: 30upx;" v-if="!scale1">转换余额</text>
				
			</view>
			<view class="un-money-item2">
				<text class="clamp un-money-item-text3">余额</text>
				<text class="un-money-item-text1">{{ecoupon1}}</text>
				<text class="clamp un-money-item-text3" style="color: #E02E24;" v-if="state!=1&&ecoupon1>0&&scale1">可转换</text>
				<text class="clamp un-money-item-text3" style="color: #F04844;" v-if="state==1">审核中</text>
				<text class="clamp un-money-item-text3" style="color: #666666;" v-if="!scale1">暂不可转换</text>
			</view>
		</view>
		<view class="un-ti">
			<text class="clamp un-ti-text" v-if="scale1">【惠券转余额规则】：</text>
			<text class="un-ti-text " v-if="scale1&&user.TAICI>=4">经纪人及以上等级转换比例：{{scale1}}%；经纪人以下等级转换比例：{{scale1/2}}%。</text>
			<text class="un-ti-text" v-if="scale1&&user.TAICI<4">经纪人及以上等级转换比例：{{scale1*2}}%；经纪人以下等级转换比例：{{scale1}}%。</text>
		</view>
		<view class="line" >
			<list-cell   title="惠券记录"   @eventClick="navTo('/pages/growup/jypointRecord')"  ></list-cell>
			<list-cell   title="电子券记录"   @eventClick="navTo('/pages/growup/exchangeEcouponRecord')"  ></list-cell>
			<list-cell   title="余额转换记录"   @eventClick="navTo('/pages/growup/exchangeMoneyRecord')"  ></list-cell>
		</view>
	</view>
</template>

<script>
	import listCell from '@/components/mix-list-cell';
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	var  graceChecker = require("../../common/graceChecker.js");
	import {  mapState  } from 'vuex'; 
	export default {
		components: {
			listCell,
			uniPopup
		},
		data(){
			return {
				coupon:0,//七日内获得惠券
				scale : '',//兑换比例
				scale1 : '',//转换余额比例
				ecoupon:'',//可兑换电子券数量
				ecoupon1:'',//可转换余额
				user:{},//用户信息，主要使用惠券，正在审核的电子券数量
				state:null,//惠券转换余额状态
				result:0.00//余额
			}
		},
		onLoad(){
			this.loadInfo()
			uni.startPullDownRefresh();
		},
		onShow() {
			this.loadInfo()
		},
		onPullDownRefresh() {
			console.log('refresh');
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		watch:{
		},
		computed: {
			...mapState(['hasLogin','userInfo']),
		},
		methods: {
			//跳转
			navTo(url){
				if(!this.hasLogin){
					// #ifdef MP-WEIXIN
					uni.showModal({
						content:'请先登录',
						success: function (res) {
						        if (res.confirm) {
						            uni.switchTab({
						            	url:"/pages/user/user"
						            })
						        } else if (res.cancel) {
						            console.log('用户点击取消');
						        }
						    }
					})
					// #endif
					// #ifndef MP-WEIXIN
					uni.navigateTo({
						url:'/pages/public/login1'
					})  
					// #endif
				}else{
					uni.navigateTo({
						url,
					})  
				}
				
			}, 
			numSub(num1, num2) {
				console.log("LLLLLLLLLLLLLLL",num1);
				console.log("LLLLLLLLLLLLLLL",num2);
				var baseNum, baseNum1, baseNum2;
				var precision;// 精度 
				try {
					baseNum1 = num1.toString().split(".")[1].length;
				} catch (e) {
					baseNum1 = 0;
				}
				console.log("LLLLLLLLLLLLLLL",baseNum1);
				try {
					baseNum2 = num2.toString().split(".")[1].length;
				} catch (e) {
					baseNum2 = 0;
				}
				console.log("LLLLLLLLLLLLLLL",baseNum2);
				baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
				console.log("LLLLLLLLLLLLLLL",baseNum);
				precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2;
				
				return ((num1 * baseNum - num2 * baseNum) / baseNum)
						.toFixed(precision);
			},
			//查询用户信息
			async loadInfo(){
				this.$http.post('/user/info',{
					SIGNATURE : uni.getStorageSync("SIGNATURE"),
					TOKEN: uni.getStorageSync("userInfo").TOKEN,
				}).then((res1)=>{
					//判断返回内容
					if(res1.data.STATE === "0" && res1.statusCode === 200){
						this.user = res1.data.OBJECT;
						
						//查询可用惠券额
						this.$http.post('/user/queryViablejypoint',{
							SIGNATURE : uni.getStorageSync("SIGNATURE"),
							TOKEN: uni.getStorageSync("userInfo").TOKEN,
						}).then((res3)=>{
							//判断返回内容
							if(res3.data.STATE === "0" && res3.statusCode === 200){
								this.coupon=res3.data.OBJECT.jypointe;
								if(!this.coupon){
									uni.showToast({
											title:'可用惠券查询异常，请稍后重试',
											icon:'none'
										})
										return
								}
							}else{
								uni.showToast({
										title:'可用惠券查询异常，请稍后重试',
										icon:'none'
									})
									return
							}
							this.result = this.numSub(this.user.JYPOINT,this.coupon)
							console.log("LLLLLLLLLLLLLLLLLLL",this.result);
						})
						.catch((err3)=>{
							console.log('request fail', err3);
						})
						
						//查询兑换比例
						this.$http.post('/common/queryScale',{
							SIGNATURE : uni.getStorageSync("SIGNATURE"),
						}).then((res4)=>{
							//判断返回内容
							if(res4.data.STATE === "0" && res4.statusCode === 200){
								if(res1.data.OBJECT.TAICI<4){
									this.scale1=res4.data.OBJECT.scale1/2;
								}else{
									this.scale1=res4.data.OBJECT.scale1;
								}
								if(this.scale1){
									let ecouponStr=parseFloat((this.user.JYPOINT-this.coupon)*(this.scale1/100)).toFixed(3);
									this.ecoupon1=ecouponStr.substring(0,ecouponStr.length-1);
								}
								if(res4.data.OBJECT.scale.SCALEVALUE){
									this.scale=res4.data.OBJECT.scale.SCALEVALUE;
								}else{
									this.scale=res4.data.OBJECT.scale;
								}
								if(this.scale){
									this.ecoupon=Math.floor((this.user.JYPOINT-this.coupon)/this.scale);
								}
							}else{
								uni.showToast({
									title: res4.data.MSG,
									icon: "none",
								})
							}
						})
						.catch((err1)=>{
							console.log('request fail', err1);
						})
						
						
						//查询惠券转换余额状态
						this.$http.post('/user/queryExchangeMoneyState',{
							SIGNATURE : uni.getStorageSync("SIGNATURE"),
							TOKEN: uni.getStorageSync("userInfo").TOKEN,
						}).then((res2)=>{
							
							//判断返回内容
							if(res2.data.STATE === "0" && res2.statusCode === 200){
								this.state=res2.data.OBJECT.STATE;
								if(this.state==1){
									this.ecoupon1=res2.data.OBJECT.MONEY;
								}
							}else{
								this.state=0;
							}
						})
						.catch((err2)=>{
							console.log('request fail', err2);
						})
						
						
					}else{
						// #ifdef MP-WEIXIN
						uni.showModal({
							content:'请先登录',
							success: function (res) {
							        if (res.confirm) {
							            uni.switchTab({
							            	url:"/pages/user/user"
							            })
							        } else if (res.cancel) {
							            console.log('用户点击取消');
							        }
							    }
						})
						// #endif
						// #ifndef MP-WEIXIN
						uni.navigateTo({
							url: "/pages/public/login1"
						})
						// #endif
					}
				})
				.catch((err2)=>{
					console.log('request fail', err2);
				})
			},
			
			//执行兑换，更改兑换状态，正在审核的电子券数量，记录兑换信息
			exchange(){
				uni.showModal({
					content: '确认将惠券兑换成电子券？',
					success: (e)=>{
						if(e.confirm){
							if(this.user.TAICI<4){
								uni.showToast({
									title:"等级不足，暂不可兑换",
									icon:"none"
								})
							}else{
								this.$http.post('/user/exchangeEcoupon',{
									SIGNATURE : uni.getStorageSync("SIGNATURE"),
									TOKEN: uni.getStorageSync("userInfo").TOKEN,
									ECOUPON:this.ecoupon,
									SCALE:this.scale,
								}).then((res1)=>{
									//判断返回内容
									if(res1.data.STATE === "0" && res1.statusCode === 200){
										this.loadInfo()
									}else{
										uni.showToast({
											title: res1.data.MSG,
											icon: "none",
										})
									}
								})
								.catch((err1)=>{
									console.log('request fail', err1);
								})
							}
						}
					}
				})
				
			},
			//执行转换，更改转换状态，正在审核的余额数量，记录转换信息
			exchange1(){
				uni.showModal({
					content: '确认将惠券转换成余额？',
					success: (e)=>{
						if(e.confirm){
							this.$http.post('/user/exchangeMoney',{
								SIGNATURE : uni.getStorageSync("SIGNATURE"),
								TOKEN: uni.getStorageSync("userInfo").TOKEN,
								ECOUPON:this.ecoupon1,
								SCALE:this.scale1,
							}).then((res1)=>{
								//判断返回内容
								if(res1.data.STATE === "0" && res1.statusCode === 200){
									this.loadInfo()
								}else{
									uni.showToast({
										title: res1.data.MSG,
										icon: "none",
									})
								}
							})
							.catch((err1)=>{
								console.log('request fail', err1);
							})
						}
					}
				})
				
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #F5F5F5;
	}
	.un-money-tou{
		width: 100%;
		background-color: #E02E24;
		height: 235upx;
		padding: 0upx 50upx;
		position: relative;
		.un-money-tou-text{
			padding-top: 40upx;
			font-size: 50upx;
			color: #fff;
		}
	}
	.un-money-itema{
		margin:220upx 30upx 0upx;
		color: #999999; 
		background-color: #fff;
		border-radius: 20upx;
		display: flex;
		padding: 40upx 0upx;
		width: 690upx;
	}
	.un-money-item{
		position: absolute;
		top:150upx;
		color: #999999; 
		background-color: #fff;
		border-radius: 20upx;
		display: flex;
		padding: 40upx 0upx;
		width: 690upx;
		left: 30upx;
		
	}
	.un-money-item1{
		width: 33.33%;
		text-align: center;
		.un-money-item-text4{
			padding: 10upx 0upx;
			border-radius: 10upx;
			border: #E02E24 1upx solid;
			color: #E02E24;
			font-size: 25upx;
			width: 150upx;
			margin:40upx;
		}
	}
	.un-money-item2{
		width: 33.33%;
		text-align: center;
		.un-money-item-text5{
			padding: 10upx 0upx;
			border-radius: 10upx;
			border: #E02E24 1upx solid;
			color: #FFFFFF;
			background-color: #E02E24;
			font-size: 25upx;
			width: 150upx;
			
		}
	}
	.un-money-item-text1{
		color: #666666;
	}
	.un-money-item-text3{
		font-size: 25upx;
		margin: 10upx 0upx;
	}
	
	.un-ti{
		margin: 10upx 50upx;
		color: #666666;
		.un-ti-text{
			font-size: 25upx;
			margin: 10upx 0upx 0upx;
		}
	}
	.line{
		color: rgb(153, 153, 153); 
		background-color: #fff;
		margin: 20upx 30upx 50upx;
		border-radius: 20upx;
	}
</style>
